package com.moli.honjie.s002test.ui;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.clj.fastble.data.BleDevice;
import com.moli.honjie.s002test.R;
import com.moli.honjie.s002test.adapter.BleScanAdapter;
import com.moli.honjie.s002test.presenter.MainActivityPresenter;
import com.moli.honjie.s002test.utils.BleUtils;

/**
 * @author Timmy
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private BleUtils mBleUtils;
    public BleScanAdapter mLeftRecyclerViewAdapter;
    public BleScanAdapter mRightRecyclerViewAdapter;
    private ProgressDialog mProgressDialog;
    private MainActivityPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.start_scan).setOnClickListener(this);

        mBleUtils = BleUtils.getInstance();

        RecyclerView leftRecyclerView = findViewById(R.id.left_recyclerview);
        RecyclerView rightRecyclerView = findViewById(R.id.right_recyclerview);

        leftRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        rightRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mLeftRecyclerViewAdapter = new BleScanAdapter(null);
        mRightRecyclerViewAdapter = new BleScanAdapter(null);
        leftRecyclerView.setAdapter(mLeftRecyclerViewAdapter);
        rightRecyclerView.setAdapter(mRightRecyclerViewAdapter);

        mLeftRecyclerViewAdapter.setOnItemClickListener(mLeftOnItemClickListener);
        mRightRecyclerViewAdapter.setOnItemClickListener(mRightOnItemClickListener);

        mPresenter = new MainActivityPresenter(this);
    }

    @Override
    public void onClick(View view) {
        // 点击开始搜索按钮
        if (mBleUtils.isBlueEnable()) {
            mPresenter.startScan();
        } else {
            Toast.makeText(this, "未打开蓝牙", Toast.LENGTH_SHORT).show();
        }
    }

    private BaseQuickAdapter.OnItemClickListener mLeftOnItemClickListener = new BaseQuickAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            // 连接设备
            BleDevice item = (BleDevice) adapter.getItem(position);

            if (item != null) {
                mPresenter.connectDevice(item);
            }
        }
    };
    private BaseQuickAdapter.OnItemClickListener mRightOnItemClickListener = new BaseQuickAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            adapter.remove(position);
        }
    };

    public void showProgressDialog(String title, String message) {
        if (mProgressDialog == null) {
            mProgressDialog = ProgressDialog.show(this, title, message, false);
        } else if (mProgressDialog.isShowing()) {
            mProgressDialog.setTitle(title);
            mProgressDialog.setMessage(message);
        }
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.show();
    }

    public void hideProgressDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBleUtils.onDestroy();
    }
}
